﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Collections;
using System.Collections.ObjectModel;

namespace ResourceTranslator
{
    /// <summary>
    /// Interaction logic for SatelliteCreationWindow.xaml
    /// </summary>
    public partial class SatelliteCreationWindow : Window
    {
        public SatelliteCreationWindow()
        {
            InitializeComponent();

            resourcesListBox.ItemsSource = resourceFiles;
        }

        private void BrowseKey_BtnClick(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "KeyFiles|*.snk;*.pfx";
            fd.Multiselect = false;
            if (true == fd.ShowDialog())
            {
                keyfileTextBox.Text = fd.FileName;
            }
        }

        private void CreateSatellite_BtnClick(object sender, RoutedEventArgs e)
        {
            string[] resFiles = new List<string>(resourceFiles).ToArray();
            Version version = new Version(versionTextBox.Text);
            string keyfile = keyfileTextBox.Text;
            string outputFile = outputpathTextBox.Text;
            string culture = cultureTextBox.Text;

            try
            {
                SatelliteAssemblyUtil.CreateAssembly(outputFile, resFiles, version, culture, keyfile);
                MessageBox.Show("Satellite Assembly Created!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Creating Satellite Assembly:\r\n", ex.Message);
            }
        }

        private void BrowseOutput_BtnClick(object sender, RoutedEventArgs e)
        {
            SaveFileDialog fd = new SaveFileDialog();

            fd.Filter = "Satellite Assembly|*.dll";
            fd.AddExtension = true;
            fd.OverwritePrompt = true;

            if (true == fd.ShowDialog())
            {
                outputpathTextBox.Text = fd.FileName;
            }
        }
        ObservableCollection<string> resourceFiles = new ObservableCollection<string>();
        private void AddResources_BtnClick(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Resource Files (*.resource)|*.resources";
            fd.Multiselect = true;
            if (true == fd.ShowDialog())
            {
                foreach (var item in fd.FileNames)
                {
                    if (resourceFiles.Contains(item))
                        continue;
                    resourceFiles.Add(item);
                }
            }
        }

        private void RemoveResources_BtnClick(object sender, RoutedEventArgs e)
        {
            //List<string> filesToRemove = new List<string>();
            resourcesListBox.BeginInit();
            foreach (string file in resourcesListBox.SelectedItems)
            {
                resourceFiles.Remove(file);
            }
            resourcesListBox.EndInit();
        }
    }

    //public static class ListUtil
    //{
    //    public static T[] GetArray<T>(this IEnumerable<T> collection)
    //    {
    //        return new List<T>(collection).ToArray();
    //    }
    //}
}
